package com.hzqy.service.epg;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.epg.MovieAndSerialDao;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.activity.ActivityVoteUserVo;

@Service
public class MovieAndSerialServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private MovieAndSerialDao masDao;

	/**
	 * 查询排行榜
	 * @param ccv
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> selectMASList(String f_cc_obligate2,String f_avu_activityid) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_cc_obligate2", f_cc_obligate2);
		map.put("f_avu_activityid", f_avu_activityid);
		List<CmContentVo> results =masDao.selectMASList(map);
		String fasid = getStart();
		for (CmContentVo cmContentVo : results) {
			cmContentVo.setF_cc_name(cmContentVo.getF_cc_name().replaceAll("HD-", ""));
		}
		map.put("fasid", fasid);
		map.put("rows", results);
		return map;
	}

	/**
	 * 插入投票记录
	 * @param acvote
	 * @return
	 */
	public int insertMASVoteUser(ActivityVoteUserVo acvote) {
		return masDao.insertMASVoteUser(acvote);
	}
	/**
	 * 查询票数
	 * @param avu
	 * @return
	 */
	public Map<String,Object> selectMASVotenum (ActivityVoteUserVo avu ) {
		Map<String,Object> map = new HashMap<String,Object>();
		int toatl=0;
		int used =0;
		List<ActivityVoteUserVo> avulist = masDao.selectMASVotenum(avu);
		for(int i=0;i<avulist.size();i++ ) {
			if (avulist.get(i).getF_avu_type() == 2) {
				used = avulist.get(i).getF_catalog_id();
			}
			if (avulist.get(i).getF_avu_type() == 3) {
				toatl = avulist.get(i).getF_catalog_id();
			}
		}
		map.put("toatl", toatl);
		map.put("used", used);
		if(used > toatl) {
			map.put("surplus", 0);
		}else {
			map.put("surplus", toatl-used);
		}
		return map;
	}
	
	/**
	 * 查询用户投票总数及排序
	 * @param avu
	 * @return
	 */
	public List<ActivityVoteUserVo> selectSisterVotenum (ActivityVoteUserVo avu ) {
		return masDao.selectSisterVotenum(avu);
	}
	
	public String getStart() {
		String flag="start";
		Date date =new Date();
		Date date2 =new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date2 = sdf.parse("2019-02-24 23:59:59");
		} catch (ParseException e) {
			
		}
		if (date2.getTime() < date.getTime()) {
			flag="end";
		}
		return flag;
	}

}
